package com.ty.zls.util;


import android.content.Context;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 请求发送util
 */
public class HttpUtil {
    private static  String host = "http://demo.zgzls.cn/app";
//    private static  String host = "http://tcsp.zgzls.cn/app";
//    private static String host = "http://192.168.0.112/app";
    // 登陆请求
    public static String LOGIN = host + "/login/doLogin";
    // 未抄表用户
    public static String UN_WATER = host + "/account/unwater";
    public static String HAD_WATER = host + "/account/hadwater";
    public static String ARREAGE = host + "/account/arreage";
    public static String WATER_CHECK = host + "/water/checkWater";
    // 置后
    public static String PRE_SORT = host + "/account/presort";
    // 修改排序
    public static String EDIT_SORT = host + "/account/editsort";
    public static String EDIT_WATER = host + "/water/editWater";
    //修改手机号
    public static String EDIT_TEL = host + "/account/editTel";
    // 抄表信息查询
    public static String WATER_ADD = host + "/water/add";
    public static String NEXT_WATER = host + "/water/nextWater";
    public static String PREV_WATER = host + "/water/prevWater";
    // 计算当期水费
    public static String WATER_CALCULATE = host + "/water/calculate";
    // 抄表信息添加
    public static String WATER_POST = host + "/water/waterAdd";
    // 用户查询
    public static String SEARCH = host + "/account/search";
    // 抄表记录
    public static String WATERS = host + "/water/waters";
    public static String WATERS_RECORDS = host + "/water/waterRecords";
    // 置后用户
    public static String PRESORT_ACCOUNT = host + "/account/presortAccounts";
    // token验证
    public static String VALIDATE = host + "/login/validate";
    //代收
    public static String PAY_WATER = host + "/water/payWater";
    public static String CHECK_VERSION = host + "/login/appVersion";


    /**
     * post 请求
     */
    public static JSONObject post(Map<String, String> json, String action) throws Exception {
        Log.e("HttpUtil", json.toString());
        OkHttpClient client = new OkHttpClient();
        FormBody.Builder formBuilder = new FormBody.Builder();
        for (String val : json.keySet()) {
            formBuilder.addEncoded(val, json.get(val));
        }
        Request request = new Request.Builder().post(formBuilder.build()).url(action).build();
        Response response = client.newCall(request).execute();
        String result = response.body().string();
        Log.e("HttpUtil", result);
        return new JSONObject(result);
    }


    public static JSONObject sendNeedLoginPost(Context context, Map<String, String> json, String action) throws Exception {
        String token = StrogeUtil.getStringFraomPs(context, StrogeUtil.user, StrogeUtil.token);
        json.put("token", token);
        return post(json, action);
    }

    public static JSONObject sendMuiltPartPost(Context context, File file,String url,Map<String,String> json) throws IOException, JSONException {
        String token = StrogeUtil.getStringFraomPs(context, StrogeUtil.user, StrogeUtil.token);
        json.put("token", token);

        OkHttpClient client = new OkHttpClient();
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        for (String val : json.keySet()) {
            builder.addFormDataPart(val, json.get(val));
        }
        RequestBody requestBody = builder.addFormDataPart("file", "logo-square.png", RequestBody.create(MediaType.parse("image/png"), file))
                .build();

        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();

        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
        String result = response.body().string();
        return new JSONObject(result);
    }

}
